
package classes;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.StringTokenizer;
import java.util.Vector;

public class EditServicePanel extends JPanel {
    
    Color error_color = Color.PINK;
    Color default_color = Color.WHITE;
    
    JLabel lblPanelTitle = new JLabel("Edit Service");
    JLabel lblType = new JLabel("Service Type:");
    JLabel lblPrice = new JLabel("Price:");
    JLabel lblDescription = new JLabel("Description:");
    
    JTextField txtPrice = new JTextField();
    JTextArea txtDescription = new JTextArea();
    
    JButton btnSubmit = new JButton("Submit", new ImageIcon(getClass().getResource("/images/ok.png")));
    JButton btnCancel = new JButton("Cancel", new ImageIcon(getClass().getResource("/images/cancel.png")));
    
    PatientManagement patientController = new PatientManagement();
    Vector types = patientController.FetchServices();
    
    JComboBox cbType = new JComboBox(types);
    
    JScrollPane scrollDescription = new JScrollPane(txtDescription);
    
    JLabel lblToothNo = new JLabel("Tooth Number:");
    JTextField txtToothNo = new JTextField();
    
    JLabel lblServiceID = new JLabel("Service ID");
    JLabel lblServiceIDValue = new JLabel();
   
    
    public EditServicePanel() {
        //panel layout
        setLayout(null);
        setSize(500,500);
        setVisible(false);
        setOpaque(false);
        
        //add to panel
        add(lblPanelTitle);
        add(lblType);
        add(lblPrice);
        add(lblDescription);
        add(txtPrice);
        add(scrollDescription);
        add(btnSubmit);
        add(btnCancel);
        add(cbType);
        add(txtToothNo);
        add(lblToothNo);
        add(lblServiceID);
        add(lblServiceIDValue);
        
        //set font
        lblPanelTitle.setFont(new Font("Century Gothic", Font.BOLD, 16));
        lblType.setFont(new Font("Century Gothic", Font.BOLD, 14));
        lblPrice.setFont(new Font("Century Gothic", Font.BOLD, 14));
        lblDescription.setFont(new Font("Century Gothic", Font.BOLD, 14));
        btnSubmit.setFont(new Font("Century Gothic", Font.BOLD, 14));
        btnCancel.setFont(new Font("Century Gothic", Font.BOLD, 14));
        txtToothNo.setFont(new Font("Century Gothic", Font.PLAIN, 14));
        txtDescription.setFont(new Font("Century Gothic", Font.PLAIN, 14));
        txtPrice.setFont(new Font("Century Gothic", Font.PLAIN, 14));
        cbType.setFont(new Font("Century Gothic", Font.PLAIN, 14));
        lblToothNo.setFont(new Font("Century Gothic", Font.BOLD, 14));
        lblServiceID.setFont(new Font("Century Gothic", Font.BOLD, 14));
        lblServiceIDValue.setFont(new Font("Century Gothic", Font.BOLD, 14));
        
        //set bounds
        lblPanelTitle.setBounds(20, 20, 150, 30);
        lblServiceID.setBounds(20, 60, 150, 30);
        lblServiceIDValue.setBounds(100, 60, 150, 30);
        
        lblType.setBounds(20, 95, 150, 30);
        cbType.setBounds(20, 120, 150, 30);
        
        lblDescription.setBounds(20, 170, 150, 30);
        scrollDescription.setBounds(20, 195, 200, 80);
              
        lblToothNo.setBounds(20, 280, 150, 30);
        txtToothNo.setBounds(20, 305, 150, 30);
        
        lblPrice.setBounds(20, 340, 150, 30);
        txtPrice.setBounds(20, 365, 150, 30);
        
        btnSubmit.setBounds(20, 450, 105, 30);
        btnCancel.setBounds(375, 450, 105, 30);
        
       
        
        btnSubmit.addActionListener(new  ActionListener() {
            public void actionPerformed(ActionEvent e) {       
             checkInputs();                  
            }
        });
        
        txtDescription.addMouseListener( new MouseListener() {
             public void mousePressed(MouseEvent e) {
                txtDescription.setBackground(default_color);
             }
             public void mouseClicked(MouseEvent e) {}
             public void mouseReleased(MouseEvent e) {}
             public void mouseEntered(MouseEvent e) {}
             public void mouseExited(MouseEvent e) {}
         });
        
         txtToothNo.addMouseListener( new MouseListener() {
             public void mousePressed(MouseEvent e) {
                txtToothNo.setBackground(default_color);
             }
             public void mouseClicked(MouseEvent e) {}
             public void mouseReleased(MouseEvent e) {}
             public void mouseEntered(MouseEvent e) {}
             public void mouseExited(MouseEvent e) {}
         });
        
        txtPrice.addMouseListener( new MouseListener() {
             public void mousePressed(MouseEvent e) {
                txtPrice.setBackground(default_color);
             }
             public void mouseClicked(MouseEvent e) {}
             public void mouseReleased(MouseEvent e) {}
             public void mouseEntered(MouseEvent e) {}
             public void mouseExited(MouseEvent e) {}
         });
        
        cbType.addActionListener(new  ActionListener() {
            public void actionPerformed(ActionEvent e) {       
             if(cbType.getSelectedIndex()==0) {
                txtToothNo.setEnabled(true);
                
             }
             else if(cbType.getSelectedIndex()==1) {
                 txtToothNo.setEnabled(false);
                 txtToothNo.setText(null);
             }
             else if(cbType.getSelectedIndex()==2) {
                 txtToothNo.setEnabled(true);
                 
             }
             else if(cbType.getSelectedIndex()==3) {
                 txtToothNo.setEnabled(true);
                 
            }
        }});
        
    }
    
    public void initValue()
    {
        int row = Main.frame.viewRecord.Records.getSelectedRow();    //get the index of the selected row
        int col = 0; //get the index of the selected column
        PatientDentalFile record = patientController.ViewDentalRecord(Main.frame.viewRecord.Records.getModel().getValueAt(row, col).toString());
        lblServiceIDValue.setText(record.getServiceID());
        cbType.setSelectedItem(record.getCategoryName());
        txtDescription.setText(record.getDescription());
        txtToothNo.setText(record.getToothNo());
        txtPrice.setText(record.getPrice());
    }
    
      public void resetComp()
    {
        cbType.setSelectedIndex(0);
        txtDescription.setText(null);
        txtToothNo.setText(null);
        txtPrice.setText(null);
    }
      
         public void resetBG()
    {
       
        txtDescription.setBackground(default_color);
        txtToothNo.setBackground(default_color);
        txtPrice.setBackground(default_color);
    }
        
    
    public void checkInputs() {
        String desc = txtDescription.getText();
        String price = txtPrice.getText();
        String tooth = txtToothNo.getText();
        boolean error = false;
        
        if(desc.length() <= 0 || desc.length() >= 30) {
            txtDescription.setBackground(error_color);
            error = true;
        }
        
        if(cbType.getSelectedIndex()==0 || cbType.getSelectedIndex()==2 || cbType.getSelectedIndex()==3)
        {
            if(tooth.length() <=0 || tooth.length() >=30) {
                txtToothNo.setBackground(error_color);
                error = true;
            }
            
              StringTokenizer st = new StringTokenizer(tooth, " ");
            StringTokenizer st2;
            
            while(st.hasMoreTokens())
            {
            st2 =new StringTokenizer(tooth, " ");
             String samp = st.nextToken();
                try{
                if(samp.length() <= 0 || Integer.parseInt(samp) <= 0 || Integer.parseInt(samp) > 32){
                    txtToothNo.setBackground(error_color);
                    error = true;
                }
                }
                catch(Exception e)
                {
                     txtToothNo.setBackground(error_color);
                     error = true;
                }
               
            int ctr = 0;
            
            while(st2.hasMoreTokens())
            {
                if(samp.equals(st2.nextToken()))
                    ctr++;
            }
            
            if(ctr > 1)
            {
                 txtToothNo.setBackground(error_color);
                 error = true;
            }
            }
        }
            
        try {
        if(price.length() <= 0 || Float.parseFloat(price) <= 0 || price.length() >6) {
            txtPrice.setBackground(error_color);
            error = true;
        }
        }
        catch(Exception e)
        {
            txtPrice.setBackground(error_color);
            error = true;
        }
        
        if(error == true)
        {
            JOptionPane.showMessageDialog(this, "Please be sure to fill up the form correctly", "Error Message", JOptionPane.ERROR_MESSAGE);    
        }
        else if (error == false)
        {
             int response = JOptionPane.showConfirmDialog(null, "Are you sure?", "Confirm",
            JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
            if (response == JOptionPane.YES_OPTION) {
                Main.frame.darkBox2.setVisible(false);
                Main.frame.lightBox2.setVisible(false);
                Main.frame.editService.setVisible(false);
                 JOptionPane.showMessageDialog(this, "The service was updated successfully.", "Message", JOptionPane.INFORMATION_MESSAGE);
                patientController.EditRecord(cbType.getSelectedItem().toString(), desc, tooth, price, lblServiceIDValue.getText());
                Main.frame.viewRecord.setRecordsTable();
                resetComp();
            }
        }
    }
}
